﻿using IHISCETeim.BLL;
using IHISCETeim.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using IHISCETeim.Library;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace IHISCETeim.Web.AgentSys.VipMgr
{
    public partial class VIPAgentExchangeUpdateAudit : AgentPageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.审核 };
            }
        }
        protected override string DWZRef
        {
            get { return "VIPAgentExchangeList"; }

        }


        protected int AgentExID
        {
            get
            {
                return GetRequestValue<int>("AgentExID", 0);
            }
        }
        private VIPAgentExchangeInfo _objEx = null;
        protected VIPAgentExchangeInfo objEx
        {
            get
            {
                if (_objEx == null)
                    _objEx = VIPAgentExchangeController.GetList(AgentExID);

                return _objEx;
            }
        }

        private List<VIPAgentExchangeDetailInfo> _listDetail = null;
        protected List<VIPAgentExchangeDetailInfo> listDetail
        {
            get
            {
                if (_listDetail == null)
                    _listDetail = VIPAgentExchangeController.GetAllDetail(AgentExID);

                return _listDetail;
            }

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "edit")
            {
                add();//
            }
            bind();
        }
        private void bind()
        {
            this.rpDetail.DataSource = listDetail;
            this.rpDetail.DataBind();
        }
        private void add()
        {
            if (!HasPower(IHISCETeim.Web.Permission.审核) )
            {
                ErrorJson("你没有权限进行此操作");
                return;
            }
            VIPAgentExchangeInfo obj = Request.SerializableObject<VIPAgentExchangeInfo>();
            obj.UserId = objEx.UserId;
            obj.AgentName = objEx.AgentName;
            //obj.AgentTotalPoint = obj.TotalPoint;
            //积分判断处理
            VIPAgentExInfo exAgentObj = VIPAgentExController.GetListByUserID(obj.UserId);

            if (exAgentObj.AvailablePoint < obj.TotalPoint)
            {
                AlertJson("对不起,此经销商的积分不足于兑换此批配件.");
            }
            if (exAgentObj.CustomerExChangePoint < obj.TotalPoint)
            {
                AlertJson("对不起,此经销商的VIP客户积分兑换值不足于用在本次此批配件兑换,<br/>注：需要VIP客户先兑换，您才能申请相应的积分兑换配件操作.");
            }

            char[] caSplit = new char[] { ',' };
            string[] ItemCodes = GetRequestValue<string>("ItemCode", "").Split(caSplit);
            string[] ItemNames = GetRequestValue<string>("ItemName", "").Split(caSplit);
            string[] Prices = GetRequestValue<string>("Price", "").Split(caSplit);
            string[] Quantitys = GetRequestValue<string>("Quantity", "").Split(caSplit);

            string[] LineTotals = GetRequestValue<string>("LineTotal", "").Split(caSplit);
            string[] Points = GetRequestValue<string>("Point", "").Split(caSplit);

            List<VIPAgentExchangeDetailInfo> list = new List<VIPAgentExchangeDetailInfo>();
            for (int rowIndex = 0; rowIndex < ItemCodes.Count(); rowIndex++)
            {
                VIPAgentExchangeDetailInfo itm = new VIPAgentExchangeDetailInfo();
                itm.ItemCode = ItemCodes[rowIndex];
                itm.ItemName = ItemNames[rowIndex];
                itm.Price = Convert.ToDecimal(Prices[rowIndex]);
                itm.Quantity = Convert.ToDecimal(Quantitys[rowIndex]);

                itm.LineTotal = Convert.ToDecimal(LineTotals[rowIndex]);
                itm.LinePoint = Convert.ToInt32(Points[rowIndex]);

                list.Add(itm);
            }


            int R = VIPAgentExchangeController.UpdateAudit(obj, list);
            switch (R)
            {
                case 1:
                    SuccessJson("已成功审核.");
                    break;
                case 0:
                    ErrorJson("审核失败");
                    break;
            }
        }
    }
}